/*
 * 
 */
package fr.univtln.superplanning.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fr.univtln.superplanning.api.ISuperplanningManager;
import fr.univtln.superplanning.api.study.Department;
import fr.univtln.superplanning.api.study.Section;
import fr.univtln.superplanning.gui.MainWindow;

/**
 * This class control the GUI that manage university sections.
 * 
 * @author vincent
 * 
 */
public class SectionController implements ActionListener, IGenericController, ListSelectionListener {

	/** The db login view. */
	private MainWindow mainWindow; // view

	/** The superplanning manager. */
	private ISuperplanningManager superplanningManager;

	private Section section;

	public SectionController(ISuperplanningManager superplanningManager,
			MainWindow mainWindow, Section section) {
		this.mainWindow = mainWindow;
		this.superplanningManager = superplanningManager;
		this.section = section;
	}

	public MainWindow getMainWindow() {
		return mainWindow;
	}

	public void setMainWindow(MainWindow mainWindow) {
		this.mainWindow = mainWindow;
	}

	public ISuperplanningManager getSuperplanningManager() {
		return superplanningManager;
	}

	public void setSuperplanningManager(ISuperplanningManager superplanningManager) {
		this.superplanningManager = superplanningManager;
	}

	public Section getSection() {
		return section;
	}

	public void setSection(Section section) {
		this.section = section;
	}

	
	public void saveSection(){
		
		Department dep = superplanningManager.getStudyManager().getDepartmentByCode(mainWindow.getSectionPanelModifyPanelDepartmentComboBox().getSelectedItem().toString());
		
		if (dep != null){
		  section = new Section();
		  section.setCode(mainWindow.getSectionPanelModifyPanelCodeTextField().getText());
		  section.setDepartment(dep);
		  section.setName(mainWindow.getSectionPanelModifyPanelNameTextField().getText());
		  superplanningManager.getStudyManager().insertSection(section);
		}
	}
	
	public void initializeViewListeners() {
		// Retreive the components and add it's listener.

		// 1. For the selection / visualization
		mainWindow.getSectionPanelListsectionPanelList().addListSelectionListener(
				this);

		// 2. For the insertion / modification
		mainWindow.getSectionPanelModifyPanelNewButton().addActionListener(this);
		mainWindow.getSectionPanelModifyPanelCancelButton().addActionListener(this);
		mainWindow.getSectionPanelModifyPanelModifyButton().addActionListener(this);
		mainWindow.getSectionPanelModifyPanelAddButton().addActionListener(this);

		// TODO: where is the new button ?
	}
	public void updateCurrentSectionView() {

		String selectedSection = (String) mainWindow
				.getSectionPanelListsectionPanelList().getSelectedValue();

		// 1. Get the section by it's code
		section = superplanningManager.getStudyManager().getSectionByCode(selectedSection);

		if ( section != null) {
			// Update the view

			mainWindow.getSectionPanelModifyPanelCodeTextField().setText(
			section.getCode());
			mainWindow.getSectionPanelModifyPanelNameTextField().setText(
					section.getName());
			mainWindow.getSectionPanelModifyPanelDescriptionTextField().setText(
					section.getDescription());
		}

	}
	
	@Override
	public void updateUi() {
		section = null;
		this.initializeViewListeners();
		mainWindow.getSectionPanelListsectionPanelList().setModel(new DefaultListModel());
	
		 List<String> sections = superplanningManager.getStudyManager().getAllSectionCodes();
		 mainWindow.getSectionPanelListsectionPanelList().setListData(sections.toArray());
	        
		 List<String> departments =  superplanningManager.getStudyManager().getAllDepartmentsCode();
		 for(String s : departments ){
			 mainWindow.getSectionPanelModifyPanelDepartmentComboBox().addItem(s); 
		 }
	}

	public void clearFields(){
		mainWindow.getSectionPanelModifyPanelCodeTextField().setText("");
		mainWindow.getSectionPanelModifyPanelDescriptionTextField().setText("");
		mainWindow.getSectionPanelModifyPanelNameTextField().setText("");
	}
	
	@Override
	public void actionPerformed(ActionEvent evt) {
		if(evt.getSource() == mainWindow.getSectionPanelModifyPanelNewButton() ){
			try
			{
                this.clearFields();
                //this.updateUi();
		         
			}
			catch(Exception e ){
				e.printStackTrace();
			}
	}
			
	else if (evt.getSource() == mainWindow.getSectionPanelModifyPanelAddButton()){
				try
				{
					System.err.println("Salut jago ");
					this.saveSection();
					this.updateUi();
					System.err.println("Salut jago ");
				}
				catch(Exception e) {
					e.printStackTrace();
			    }
	}
	

	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
		
		if (e.getSource() == mainWindow.getSectionPanelListsectionPanelList()) {
			updateCurrentSectionView();
	     }
	}

}
